using Godot;
using System;

namespace EnemyStateNode
{
public partial class Run : Node,StateNode
{
	public Enemy enemy;

	[Export]
	public int minDst = 350;
    public override void _Ready()
    {		
        enemy = GetNode<Enemy>("/root/Main/Enemy");
    }			

	public void OnEnter()
	{
		enemy.GetNode<AnimatedSprite2D>("AnimaEnemy").Animation="run";	

		GetNode<AudioStreamPlayer2D>("Music").Play();
	}

	public void OnExit()
	{
		enemy.Velocity = new Vector2(0,0);
		GetNode<AudioStreamPlayer2D>("Music").Stop();
	}

	public void OnUpdate(float delta)
	{
		Vector2 velocity = new Vector2(enemy.Speed,0);
		enemy.UpdateFace();
		enemy.Velocity = enemy.FacingLeft?-velocity:velocity;	

		if(enemy.Health<=0)
			enemy.SwitchTo("Dead");
		else if((enemy.Position-enemy.player.Position).Abs().X<=minDst)
		{   
			int randNum = (int)MyUtil.RangeRandom(1,100);
			if(enemy.Health<=5)
			{						
				if(randNum<=20)
				{
					if(!enemy.IsOnFloor())
						enemy.SwitchTo("Fall");
					else
						enemy.SwitchTo("Jump");
				}
				else if (randNum <= 75)
					enemy.SwitchTo("Squat");
				else
					enemy.SwitchTo("ThrowSilk");
			}
			else
			{
				if(randNum<=20)
				{
					if(!enemy.IsOnFloor())
						enemy.SwitchTo("Fall");
					else
						enemy.SwitchTo("Jump");
				}
				else if (randNum <= 75)
					enemy.SwitchTo("ThrowSilk");
				else
					enemy.SwitchTo("Squat");					
			}
		}
	}
}
}

